$(function () {
    $("#backPage").load("/html/backPage.html");
})

// 查询所有角色
function findAll() {
    let data = {
        key: $("#selectKey").val(),
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    let result = jsonGetValue("/pm/role/getSelectList", data);
    console.log(result);
    setAllData(result.data.list);
    $("#total").html(result.data.count);
}
// 向角色列表中填充数据
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            '        <td><input type="checkbox" name="checkOne" value="' + data[i].id + '"></td>\n' +
            "        <td>" + data[i].id + '</td>\n' +
            "        <td>" + data[i].name + "</td>\n" +
            "        <td>" + data[i].description + "</td>\n" +
            "        <td>" + data[i].userCount + "</td>\n";
        if (data[i].stateCode == "enable") {
            html += "<td class='yes'><div onclick=\"changeShowState('" + data[i].id + "','disable')\"><span>" + data[i].roleState + "</span><span class='iconfont'>&#xe60d</span></div></td>\n";
        } else if (data[i].stateCode == "disable") {
            html += "<td class='no'><div onclick=\"changeShowState('" + data[i].id + "','enable')\"><span>" + data[i].roleState + "</span><span class='iconfont'>&#xe600</span></div></td>\n";
        }
        html += "    <td>" + data[i].createTime + "</td>\n" +
            "        <td>" + data[i].updateTime + "</td>\n";
        html += "    <td>\n" +
            "            <div onclick=\"distribute('"+ data[i].id +"')\">" +
            '            <a href="#distribute-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-distribute">&#xe696;</i></a></div>\n' +
            "            <div onclick=\"edit('"+ data[i].id +"')\">" +
            '            <a href="#edit-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-edit">&#xe608;</i></a></div>\n' +
            "            <div onclick=\"del('"+ data[i].id +"')\">" +
            '            <a href="#del-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-del">&#xe60b;</i></a></div>\n' +
            "        </td>\n" +
            "    </tr>";
    }
    $("#tbody").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    $("#tbody > tr > td:nth-child(6) > div").css({"cursor": "pointer","display": "flex","align-items": "center"})
    $("#tbody > tr > td:last-child").css({"display": "flex","justify-content": "flex-start"})
    $("#tbody > tr > td:last-child > div").css({"cursor": "pointer"})
    $("#tbody > tr > td:last-child > div").css({"margin-right":"10px"})
    myCheckAO();
}

// 修改角色状态
function changeShowState(id,stateCode){
    let data = {
        id: id,
        stateCode:stateCode
    };
    let result = jsonPost("/pm/role/updateState", data)
    if (result.data.result == 1) {
        page('go');
    } else {
        alert("修改状态失败");
    }
}

// 添加角色
function addRole() {
    let name =  $("#addName").val();
    let description = $("#addDescription").val();
    let data = {
        name:name,
        description: description,
    };
    if (name == null || name == "") {
        $("#nameTip").text("角色名称不能为空");
    } else if (description == null || description == "") {
        $("#nameTip").text();
        $("#descriptionTip").text("角色描述不能为空");
    } else {
        let result = jsonPost("/pm/role/addRole", data);
        console.log(result);
        if (result.data.result == "success") {
            $(".right").load("/html/role/role-list.html");
        }
    }
}

// 分配权限
function distribute(id) {
    $("#checkRoleId").val(id);
    checkRolePermission(id);
    getRoleById(id);
}

// 调用查询分配权限的方法
function checkRolePermission(id) {
    getPermissionByRoleId(id);
    getPermissionItemByNoRoleId(id);
}

// 获取已拥有权限
function getPermissionByRoleId(id) {
    let data = {roleId: id}
    let result = jsonGetValue("/pm/permission/getPermissionByRoleId",data);
    console.log(result);
    setPermissionItemDataByRoleId(result.data.data);
}
// 分配已拥有权限
function setPermissionItemDataByRoleId(data) {
    let html = "";
    for (let i=0;i<data.length;i++){
        html += "<div>\n" +
            "        <label>\n" +
            "              <input  type=\"checkbox\" name='checkPermission' value='"+ data[i].id +"'/>\n" +
            "              <span>"+ data[i].name +"</span>\n" +
            "        </label>\n" +
            "    </div>";
    }
    $("#PermissionMember").html(html);
}

// 获取未拥有权限
function getPermissionItemByNoRoleId(id) {
    let data = {roleId: id}
    let result = jsonGetValue("/pm/permission/getPermissionItemByNoRoleId",data);
    console.log(result);
    setPermissionItemDataByNoRoleId(result.data.data);
}
// 分配未拥有权限
function setPermissionItemDataByNoRoleId(data) {
    let html = "";
    for (let i=0;i<data.length;i++){
        html += "<div>\n" +
            "        <label>\n" +
            "              <input type=\"checkbox\" name='checkNoPermission' value='"+ data[i].id +"'/>\n" +
            "              <span>"+ data[i].name +"</span>\n" +
            "        </label>\n" +
            "    </div>";
    }
    $("#NoPermissionMember").html(html);
}

// 添加权限
function addPermissionMember(name) {
    let roleId = $("#checkRoleId").val();
    let data = getAddCheck(name,roleId);
    console.log(data);
    let result = jsonPost("/pm/permission/addRolePermission",data);
    console.log(result);
    if (result.data.data == "success"){
        checkRolePermission(roleId);
    }else {
        alert("操作失败");
    }
}
// 获取多选框选择的未拥有权限
function getAddCheck(name,roleId) {
    let PermissionName = [];
    $("[name='"+name+"']:checkbox").each(function(i){
        if ($(this).prop("checked") == true){
            // PermissionName.push();
            // permissionId = PermissionName[i];
            let param = {
                roleId :parseInt(roleId),
                permissionId:parseInt($(this).val())
            }
            PermissionName.push(param);
        }
    })
    // PermissionName = JSON.stringify(PermissionName);
    // PermissionName = PermissionName.replace("[","").replace("]","");
    return PermissionName;
}
// 移除权限
function removePermissionMember(name) {
    let permissionList = getDelCheck(name);
    let roleId = $("#checkRoleId").val();
    let data = {roleId:roleId,ids:permissionList};
    let result = jsonPostParam("/pm/permission/removeRolePermission",data);
    console.log(result)
    if (result.data.data == "success"){
        checkRolePermission(roleId);
    }else {
        alert("操作失败");
    }
}
// 获取多选框选择的已拥有权限
function getDelCheck(name) {
    let PermissionName = [];
    $("[name='"+name+"']:checkbox").each(function(i){
        if ($(this).prop("checked") == true){
            PermissionName.push(parseInt($(this).val()));
        }
    })
    PermissionName = JSON.stringify(PermissionName);
    PermissionName = PermissionName.replace("[","").replace("]","");
    return PermissionName;
}

// 根据id获取角色
function getRoleById(id) {
    let data = {id: id}
    let result = jsonGetValue("/pm/role/getRoleById", data);
    console.log(result);
    setRoleDataById(result.data.data);
}
// 给修改和删除模态框赋值
function setRoleDataById(data) {
    $("#updateId").val(data.id);
    $("#updateName").val(data.name);
    $("#updateDescription").val(data.description);
    $("#roleName").val(data.name);
    $("#roleDescription").val(data.description);
    $("#delId").val(data.id)
    $("#tips").html("是否删除角色:" + data.name + "?");
}

// 弹出修改模态框
function edit(id) {
    getRoleById(id);
}
// 执行修改
function doEdit() {
    let id = $("#updateId").val();
    let name = $.trim($("#updateName").val());
    let description = $.trim($("#updateDescription").val());
    let data = {
        id:id,
        name:name,
        description: description,
    };
    console.log(data);
    let result = jsonPost("/pm/role/updateRole", data);
    console.log(result);
    if (result.data.result == 1) {
        $(".right").load("/html/role/role-list.html");
    }
}

// 弹出修改模态框
function del(id) {
    getRoleById(id);
}
// 执行删除
function doDel() {
    let data = {id:$("#delId").val()}
    let result = jsonPost("/pm/role/delRole", data);
    if (result.data.result == 1) {
        $(".right").load("/html/role/role-list.html");
    }
}

// 查询点击事件
function select() {
    page("home");
}
// 清空点击事件
function clearAll() {
    $("input[ type='text']").val("");
    $("input[ type='date']").val("");
    $("#pageSize").val(10);
    $("#pageNum").val(1);
}